package com.maigao.api.controller;

import com.maigao.api.interceptors.identify.Identify;
import com.maigao.entity.ResultJson;
import com.maigao.entity.ThirdPartyCmd;
import com.maigao.entity.vo.UserVO;
import com.maigao.service.LoginService;
import com.maigao.service.UserService;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-16 23:34
 **/
@RestController
@RequestMapping(value = "/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/bind")
    public ResultJson bind(@RequestParam String mobile, @RequestParam String registrationCode) {
        Pair<Boolean, String> bindResult = userService.bindRegistrationCode(mobile, registrationCode);
        if (bindResult.getLeft()) {
            return ResultJson.success(bindResult.getRight());
        }
        return ResultJson.fail(bindResult.getRight());
    }

    @RequestMapping("/thirdParty")
    @Identify
    public ResultJson thirdParty(ThirdPartyCmd cmd) {
        Triple<Boolean, UserVO, String> loginResult = loginService.thirdParty(cmd);
        if (loginResult.getLeft()) {
            return ResultJson.success(loginResult.getMiddle());
        }
        return ResultJson.fail(loginResult.getRight());
    }
}
